package com.icerhe.springtest.mvc;

import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.TreeMap;


@Controller
public class HomeController {
	
	public static final int PAGE_SIZE = 25;
	protected static String testUrl = "http://gw.api.taobao.com/router/rest";//沙箱环境调用地址
    protected static String appkey = "21518693";
    protected static String secret = "952ef10db0d88851978af7df1981d9d5";
	@RequestMapping({"/","/home"})
	public String showHomePage(Map<String, Object> model) {
		model.put("spittles", "HelloWorld");
		
		TreeMap<String, String> apiparamsMap = new TreeMap<String, String>();
        apiparamsMap.put("format", "xml");
        apiparamsMap.put("method", "taobao.itemcats.get");
        apiparamsMap.put("sign_method","md5");
        apiparamsMap.put("app_key",appkey);
        apiparamsMap.put("v", "2.0");
        //apiparamsMap.put("session",request.getParameter("session"));他用型需要sessionkey
        String timestamp =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        apiparamsMap.put("timestamp",timestamp);
        apiparamsMap.put("fields","cid,parent_cid,name,is_parent");//需要获取的字段
        apiparamsMap.put("parent_cid","0");
        //生成签名
        String sign = Util.md5Signature(apiparamsMap,secret);
        apiparamsMap.put("sign", sign);
        StringBuilder param = new StringBuilder();
        for (Iterator<Map.Entry<String, String>> it = apiparamsMap.entrySet()
        .iterator(); it.hasNext();) {
            Map.Entry<String, String> e = it.next();
            param.append("&").append(e.getKey()).append("=").append(e.getValue());
        }
        String x = param.toString().substring(1);
        String result = Util.getResult(testUrl, x);
		return "home";
	}

}
